home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / fonttools / rawtohex.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  214 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    rawtohex -
  19.  *        decode an encrypted type1 font.
  20.  *
  21.  *                Paul Haeberli - 1990
  22.  */
  23. #include "stdio.h"
  24. #include "ctype.h"
  25.  
  26. unsigned char *charfiledata();
  27.  
  28. FILE *inf, *outf;
  29. int macfont;
  30.  
  31. main(argc,argv)
  32. int argc;
  33. char **argv;
  34. {
  35.     int d;
  36.     char str[256];
  37.     int i, temp;
  38.     int nbytes, maxpos, patlen;
  39.     unsigned char *filedata;
  40.     int beginpos, endpos, count;
  41.     int col;
  42.  
  43.     if(argc<3) {
  44.     fprintf(stderr,"usage: rawtohex in.raw out.hex\n");
  45.     exit(1);
  46.     }
  47.     inf = fopen(argv[1],"r");
  48.     if(!inf) {
  49.     fprintf(stderr,"rawtohex: can't open input file\n");
  50.     exit(1);
  51.     }
  52.     outf = fopen(argv[2],"w");
  53.     macfont = 0;
  54.     nbytes = sizeoffile(inf);
  55.     printf("nbytes is %d\n",nbytes);
  56.  
  57.     filedata = charfiledata(inf);
  58.  
  59.     patlen = strlen("currentfile eexec\r");
  60.     maxpos = nbytes-patlen;
  61.     for(i=0; i<nbytes; i++) {
  62.     if(strncmp(filedata+i,"currentfile eexec\r",patlen) == 0) {
  63.         beginpos = i+patlen;
  64.         printf("char at begin is %d\n",filedata[beginpos]);
  65.         break;
  66.     }
  67.     }
  68.     if(i == nbytes) {
  69.     fprintf(stderr,"rawtohex: eexec string not found\n");
  70.     exit(1);
  71.     }
  72.     for(i=0; i<beginpos; i++) {
  73.     if(filedata[i] == '\r')
  74.         filedata[i] = '\n';
  75.     }
  76.     fwrite(filedata,1,beginpos,outf);
  77.  
  78.     i = nbytes;
  79.     count = 0;
  80.     while(i--) {
  81.     if(filedata[i] == '0') {
  82.         count++;
  83.         if(count == 512) 
  84.         break;
  85.     } else if(!isspace(filedata[i])) {
  86.         count = 0;
  87.     }
  88.     }
  89.     if(count == 512) 
  90.     fprintf(stderr,"rawtohex: found 512 zeros at %d\n",i);
  91.     else {
  92.     fprintf(stderr,"rawtohex: can't find 512 zeros!!\n");
  93.     exit(1);
  94.     }
  95.     endpos = i;
  96.     col = 0;
  97.     for(i=beginpos; i<endpos; i++) {
  98.     fprintf(outf,"%02x",filedata[i]);
  99.     col++;
  100.     if(col == 32) {
  101.         col = 0;
  102.         fprintf(outf,"\n");
  103.     }
  104.     }
  105.     if(col != 0) 
  106.     fprintf(outf,"\n");
  107.     for(i=endpos; i<nbytes; i++) {
  108.     if(filedata[i] == '\r')
  109.         filedata[i] = '\n';
  110.     }
  111.     fwrite(filedata+endpos,1,nbytes-endpos,outf);
  112.     fclose(outf);
  113.     exit(0);
  114. }
  115.  
  116. mygets(str,inf)
  117. char *str;
  118. FILE *inf;
  119. {
  120.     char c;
  121.  
  122.     while(1) {
  123.     c = fgetc(inf);
  124.     if((c == '\r') || (c == '\n')) {
  125.         if(c == '\r')
  126.         macfont = 1;
  127.         else
  128.         macfont = 0;
  129.         *str = 0;
  130.         return 1;
  131.     }
  132.     *str++ = c;
  133.     }
  134. }
  135.  
  136. unsigned short int r;
  137. unsigned short int c1 = 52845;
  138. unsigned short int c2 = 22719;
  139.  
  140. unsigned char decrypt(cipher)
  141. unsigned char cipher;
  142. {
  143.     unsigned char plain;
  144.  
  145.     plain = (cipher^(r>>8));
  146.     r = (cipher+r)*c1 + c2;
  147.     return plain;
  148. }
  149.  
  150. nexthexchar(f)
  151. FILE *f;
  152. {
  153.     unsigned char c[1];
  154.     int n;
  155.  
  156.     while(1) {
  157.     fread(c,1,1,f);
  158.     if(c[0] != '\n')
  159.         return c[0];
  160.     }
  161. }
  162.  
  163. fromhex(c)
  164. int c;
  165. {
  166.     if(c>='0' && c<='9')
  167.      return c-'0';
  168.     if(c>='a' && c<='f')
  169.      return 10+c-'a';
  170.     if(c>='A' && c<='F')
  171.      return 10+c-'A';
  172.     fclose(outf);
  173.     exit(1);
  174. }
  175.  
  176. gethexbyte(f)
  177. FILE *f;
  178. {
  179.     int low, hi;
  180.  
  181.     hi = fromhex(nexthexchar(f));
  182.     low = fromhex(nexthexchar(f));
  183.     return ((hi<<4)+low);
  184. }
  185.  
  186. nextbyte(f)
  187. FILE *f;
  188. {
  189.     if(macfont) 
  190.     return fgetc(f);
  191.     else
  192.     return gethexbyte(f);
  193. }
  194.  
  195. tohex(f)
  196. FILE *f;
  197. {
  198.     int i, c, d;
  199.     int pos;
  200.  
  201.     pos = 0;
  202.     while(!feof(f)) {
  203.     d = nextbyte(f);
  204.     fprintf(outf,"%02x",d);
  205.     pos++;
  206.     if(pos == 32) {
  207.         fprintf(outf,"\n");
  208.         pos = 0;
  209.          }
  210.     }
  211.     fprintf(stderr,"end of file\n");
  212.     fprintf(outf,"\n");
  213. }
  214.